package com.DuoXianCheng.D10;

public class C01 extends Thread{
    // 这个类所有的对象都共享 ticket
    static int ticket = 0;
    //锁对象，一定要是唯一的
    static Object obj = new Object();
    @Override
    public void run() {
        while (true) {
            synchronized (obj){
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                if(ticket < 100){
                    ticket++;
                    System.out.println(getName() + "正在买第" + ticket + "张票!");
                } else {
                    break;
                }
            }
        }
    }
}
